<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
</head>
<body>
    <button id="btn1">存储数据</button>
    <button id="btn2">查询数据</button>
    <button id="btn3">删除数据</button>
    <button id="btn4">清空数据</button>

    <script>
        //保存数据
        $("#btn1").on('click',function(){
            sessionStorage.setItem('name','张三')
            //数字会自动转换为字符串 = 调用toString方法
            sessionStorage.setItem('age',21)
            //传入对象
            let obj = {
                name:'苏凉',
                address:'tjcu'
            }
            sessionStorage.setItem("obj",JSON.stringify(obj))
            // console.log(this);
        })
        //读取数据
        $("#btn2").on('click',function(){
            console.log(sessionStorage.getItem('name'));
            console.log(sessionStorage.getItem('age'));
            //读取对象
            let person = sessionStorage.getItem('obj')
            console.log(JSON.parse(person));
            // console.log(this);
        })
        //删除数据
        $("#btn3").on('click',function(){
            sessionStorage.removeItem('age')
            // console.log(this);
        })
        //清空数据
        $("#btn4").on('click',function(){
            sessionStorage.clear();
            // console.log(this);
        })
    </script>
</body>
</html>